//
//  DoctorInfoController.m
//  kuaiyiDemo
//
//  Created by vn on 16/7/7.
//  Copyright © 2016年 vn. All rights reserved.
//

#import "DoctorInfoController.h"
#import "DoctorInfoHeadView.h"
#import "Masonry.h"
#import "SEDoctorInfoCenterView.h"
@interface DoctorInfoController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate,centerViewDeiegate>



//头部View
@property(nonatomic,strong)DoctorInfoHeadView * headView;
//线条view
@property(nonatomic,strong)UIView * lineView;
//中间View
@property(nonatomic,strong)SEDoctorInfoCenterView * centerView;
//底部collection
@property(nonatomic,strong)UICollectionView * collectionView;
//底部咨询医生按钮
@property(nonatomic,strong)UIButton * consult_btn;
//流水布局
@property(nonatomic,strong)UICollectionViewFlowLayout * flowLayout;


@end

@implementation DoctorInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"医生基本信息";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpUI];
    [self setUpCollectionView];
    [self setUplayout];
   
    //注册cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark -- 创建tableView方法
 - (UITableView *)createTableViewWith:(UITableViewStyle)style{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:style];
    
     tableView.delegate = self;
     tableView.dataSource = self;
    
   
     tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.collectionView.bounds.size.width);
     
     

    return tableView;

}
#pragma mark -- collectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 2;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    if (indexPath.row == 0) {
        
       UITableView * plainTableView = [self createTableViewWith:UITableViewStylePlain];
             plainTableView.tag = 1;
         [cell.contentView addSubview:plainTableView];
        
        
    }else{
        
        
        UITableView * groupTableView = [self createTableViewWith:UITableViewStyleGrouped];
        groupTableView.tag = 2;
        [cell.contentView addSubview: groupTableView];

    }
    
    return cell;
    

}

#pragma mark -- tableView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
    
    if (index == 1) {
        
        
        [self.centerView.doctorInfo_btn setTitleColor: kThemeColor forState:UIControlStateNormal];
        [self.centerView.condition_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        
    }else{
        
        
        [self.centerView.doctorInfo_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self.centerView.condition_btn setTitleColor:kThemeColor forState:UIControlStateNormal];

    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat  scrollViewOfset = scrollView.contentOffset.x ;
    
    
    //防止tableView上下滑动的调用这个方法,
    if ([scrollView isKindOfClass:[UITableView  class]]) {
        
        return;
        
    }
    
    [UIView animateWithDuration:0.5 animations:^{
       
        
        self.centerView.moveView.transform = CGAffineTransformMakeTranslation(scrollViewOfset * 0.5, 0);
        
    }];
    
    
}

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (tableView.tag == 2) {
        
        
        return 5;
    }
    
      return 1;

 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView.tag == 2) {
        
        return 1;
        
    }
    
      return 9;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
        
        reuseIdentifier:@"cell"];
        
    }
    
    
    if (tableView.tag == 1) {
        
        cell.textLabel.text = [NSString stringWithFormat:@"就诊条件: %ld行",indexPath.row];
        
    }else if (tableView.tag == 2){
        
        
        
        cell.textLabel.text = [NSString stringWithFormat:@"医生简介: %ld",indexPath.section];
        
    }
    
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if (tableView.tag == 1) {
        
        return 0;
    }
    
    return 15;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 1) {
        
        return 40;
    }else{
        
        return 40;
        
    }
    
    
}

#pragma mark -- 代理方法;
- (void)centerView:(SEDoctorInfoCenterView *)centerView withButton:(UIButton *)sender{
    
    
    if (sender.tag == 1) {
        
        
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        
        [UIView animateWithDuration:0.5 animations:^{
            
//
            
            self.centerView.moveView.transform = CGAffineTransformMakeTranslation(self.collectionView.contentOffset.x * 0.5, 0);
            
            
            
        [self.centerView.doctorInfo_btn setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
        [self.centerView.condition_btn setTitleColor:kThemeColor forState:UIControlStateNormal];
            
        }];
        
    }else{
        
        
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            
            
                self.centerView.moveView.transform = CGAffineTransformMakeTranslation(self.collectionView.contentOffset.x * 0.5, 0);
            
            
        [self.centerView.doctorInfo_btn setTitleColor:kThemeColor forState:UIControlStateNormal];
        [self.centerView.condition_btn setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
            
        }];
        
     }

    
}

#pragma mark -- 布局子空间
- (void)setUplayout{
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.offset(150);
        make.leading.trailing.equalTo(self.view);
        make.top.offset(49);
        
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_offset(3);
        make.leading.trailing.equalTo(self.view);
        make.top.mas_equalTo(self.headView.mas_bottom);
        
    }];
    
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.leading.trailing.mas_equalTo(self.view);
        make.height.offset(50);
        
    }];


    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.centerView.mas_bottom).offset(10);
        make.leading.trailing.equalTo(self.centerView);
        make.bottom.equalTo(self.consult_btn.mas_top).offset(-10);
        
    }];
    
    
    [self.consult_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.view).offset(-5);
        make.leading.equalTo(self.view).offset(5);
        make.trailing.equalTo(self.view).offset(-5);
        make.height.mas_offset(40);
        
    }];

}


#pragma mark -- 布局flowLayout
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.flowLayout.itemSize = self.collectionView.frame.size;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    
  
}

#pragma mark -- 初始化collectionView
- (void)setUpCollectionView{
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowLayout = flowLayout;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout] ;
    self.collectionView.delegate =self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
}

#pragma mark -- 初始化控件
- (void)setUpUI{
    
    [self.view addSubview: self.centerView];
    [self.view addSubview:self.consult_btn];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.lineView];

}

#pragma mark -- lazy控件
 - (SEDoctorInfoCenterView *)centerView{
    if (_centerView == nil) {
        
     _centerView = [[SEDoctorInfoCenterView alloc]init];
    _centerView.delegate = self;
        
    }
    
    return _centerView;
    
}

- (DoctorInfoHeadView *)headView{
    
    if (_headView == nil) {
        
        _headView = [[DoctorInfoHeadView alloc]init];
        _headView.focusModel = self.fochusModel;

    }
    
    return _headView;
}

 - (UIButton *)consult_btn{
    
    if (_consult_btn == nil) {
        
        
        _consult_btn = [[UIButton alloc]init];
        _consult_btn.layer.cornerRadius = 5;
       _consult_btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_consult_btn setTitle:@"咨询医生" forState:UIControlStateNormal];
        [_consult_btn setBackgroundColor:kThemeColor];

        
        
    }
    
    return _consult_btn;
    
}

- (UIView *)lineView{
    
    if (_lineView == nil) {
        
       
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor grayColor];

    }
    
    return _lineView;
    
}

@end
